// pages/specifications/specifications.js
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cpgg: [],
        speclist: [],

        additem: false,
        additemname: '',
        additemchild: false,
        additemchildname: '',
        additemchildselect: 0,
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    onClose() {
        this.setData({
            additem: false,
            additemname: '',
            additemchild: false,
            additemchildname: '',
        })
    },
    additemopen() {
        this.setData({
            additem: true,
        })
    },
    additemchildopen(items) {
        this.setData({
            additemchild: true,
            additemchildselect: items.currentTarget.dataset.select,
        })
    },
    navestrati() {
        wx.navigateTo({
            url: '../strationspecifications/strationspecifications',
        })
    },
    async getspeclist() {
        let resdata = await basesup.speclist()
        this.setData({
            speclist: resdata.data.Result.records
        })
    },
    addone(items) {
        let array = {
            name: '',
            params: []
        }
        for (const aa in this.data.cpgg) {
            if (this.data.cpgg[aa].name == this.data.speclist[items.currentTarget.dataset.select].name) {
                wx.showToast({
                    title: '已经添加',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
        }
        array.name = this.data.speclist[items.currentTarget.dataset.select].name
        array.params = JSON.parse(this.data.speclist[items.currentTarget.dataset.select].params)
        let newlist = this.data.cpgg
        newlist.push(array)
        this.setData({
            cpgg: newlist
        })
    },
    additemconfig() {
        if (this.data.additemname == '' || this.data.additemname == null || this.data.additemname == undefined) {
            wx.showToast({
                title: '未填写信息',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let array = {
            name: '',
            params: []
        }
        for (const aa in this.data.cpgg) {
            if (this.data.cpgg[aa].name == this.data.additemname) {
                wx.showToast({
                    title: '已经添加',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
        }
        array.name = this.data.additemname
        array.params = []
        let newlist = this.data.cpgg
        newlist.push(array)
        this.setData({
            cpgg: newlist
        })
        this.onClose()
    },
    additemchildconfig(items) {
        if (this.data.additemchildname == '' || this.data.additemchildname == null || this.data.additemchildname == undefined) {
            wx.showToast({
                title: '未填写子规格信息',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let array = {
            name: '',
        }
        for (const aa in this.data.cpgg[this.data.additemchildselect].params) {
            if (this.data.cpgg[this.data.additemchildselect].params[aa].name == this.data.additemchildname) {
                wx.showToast({
                    title: '已经添加',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
        }
        let setdata = `cpgg[${this.data.additemchildselect}].params`
        array.name = this.data.additemchildname
        let newlist = this.data.cpgg[this.data.additemchildselect].params
        newlist.push(array)
        console.log(setdata)
        this.setData({
            [setdata]: newlist,
        })

        this.onClose()
    },
    delitem(items) {
        let setdatas = `cpgg`
        let dellist = this.data.cpgg;
        dellist.splice(items.currentTarget.dataset.select, 1)
        this.setData({
            [setdatas]: dellist
        });
    },
    delitemchild(items) {
        let setdatas = `cpgg[${items.currentTarget.dataset.select}].params`
        let dellist = this.data.cpgg[items.currentTarget.dataset.select].params;
        dellist.splice(items.currentTarget.dataset.childselect, 1)
        this.setData({
            [setdatas]: dellist
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getspeclist()
        this.setData({
            cpgg: JSON.parse(options.cpgg)
        })
    },
    backsetdata() {
        for (const keys in this.data.cpgg) {
            if (this.data.cpgg[keys].params.length == 0) {
                wx.showToast({
                    title: '含有未输入的子规格',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
        }
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            "cpgg": this.data.cpgg,
        })
        setTimeout(() => {
            this.naveback()
        }, 300);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getspeclist()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})